<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
  <th:block th:replace="headfoot/head::head_ztree">引用公共的homefoot</th:block>
</head>
<body>
	<div class="container-fluid p-t-15">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
					<div class="card-body">
						<div class="card-toolbar clearfix">
							<button type="button" class="btn btn-default btn-sm kj-tree-btn-add" onclick="addNode()">
								<i class="iconfont icon-tianjia"></i>&ensp;添加
							</button>
			
							<button type="button" class="btn btn-default btn-sm kj-tree-btn-edit" onclick="editNode()">
								<i class="iconfont icon-bianjibiaodan"></i>&ensp;编辑
							</button>
			
							<button type="button" class="btn btn-default btn-sm kj-tree-btn-del" onclick="removeNode()">
								<i class="iconfont icon-shanchu"></i>&ensp;删除
							</button>
						</div>
						<div>
							<ul
								class="ztree"
								data-url="/system/resource/resourceForm"
								data-setting='resourceTreeSetting'
							></ul>
						</div>
					</div>
				</div>	
			</div>
		</div>
	</div>
	
</body>
<script th:inline="javascript" type="text/javascript">
         
        
        function filter(treeId, parentNode, childNodes) {
        	if (!childNodes) return null;
        	childNodes = childNodes.data;
        	for (var i=0, l=childNodes.length; i<l; i++) {
        		if(childNodes[i].name){
        			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
        		}
        	}
        	return childNodes;
        }
        
		var resourceTreeSetting = {
			"async": {
					"enable": true,
					"url": "/system/resource/resourceForm",
					"autoParam": [
					              "id",
					              "name=n",
					              "level=lv"
								],
        			"otherParam": {
        				"otherParam": "zTreeAsyncTest"
        			},
        			"dataFilter": filter
        	},
        	"check": {
        		"enable": false,
        		"chkStyle": "checkbox",
        		"chkboxType": {
        			"Y": "ps",
        			"N": "ps"
        		}
        	}
        };
        
        function addNode() {
        	var treeObj = $(".ztree").data("ztree");
        	var nodes = treeObj.getSelectedNodes();
        	var pid;
        	if (!nodes || nodes.length === 0) {
	        	pid = "";
        	} else if (nodes.length == 1) {
	        	pid = nodes[0].id;
        	} else {
        		common.confirm("请选择一个节点!", null, {title: "提示"});
		        return;
        	}
        	
        	var url = "url:" + base_url + "/system/resource/showEdit?pid=" + pid;
        	common.confirm(url, function() {
      			if (!$("#resourceForm").valid()) {
      				return false;
      			}
      			
      			var resultFlag = false;
      			$("#resourceForm").ajaxSubmit({
      				async: false,
      				url: base_url + "/system/resource/saveOrUpdate",
      				dataType: "json",
      				success: function(data) {
      					if (data.code == 0) {
      						resultFlag = true;
      					}
      					common.alert(data, function() {
      						treeObj.reAsyncChildNodes(nodes[0], "refresh", null);
      					}, true);
      				}
      			});
      			return resultFlag;
        	}, {title: '添加权限', closeIcon: true,columnClass: 'col-md-10 col-md-offset-0'});
        }
        
        function editNode() {
        	var treeObj = $(".ztree").data("ztree");
        	var nodes = treeObj.getSelectedNodes();
        	if (nodes.length != 1) {
        		common.confirm("请选择一个节点!", null, {title: "提示"});
		        return;
        	}
        	var id = nodes[0].id;
        	
        	var url = "url:" + base_url + "/system/resource/showEdit?id=" + id;
        	common.confirm(url, function() {
        		if (!$("#resourceForm").valid()) {
    				return false;
    			}
    			
    			var resultFlag = false;
    			$("#resourceForm").ajaxSubmit({
    				async: false,
    				url: base_url + "/system/resource/saveOrUpdate",
    				dataType: "json",
    				success: function(data) {
    					if (data.code == 0) {
    						resultFlag = true;
    					}
    					common.alert(data, function() {
    						treeObj.reAsyncChildNodes(nodes[0].getParentNode(), "refresh", null);
    					}, true);
    				}
    			});
    			return resultFlag;
        	}, {title: '编辑权限', closeIcon: true,columnClass: 'col-md-10 col-md-offset-0'})
        }
        
        function removeNode() {
        	var treeObj = $(".ztree").data("ztree");
        	var nodes = treeObj.getSelectedNodes();
        	if (nodes.length == 0) {
        		common.confirm("请选择节点!", null, {title: "提示"});
		        return;
        	}
        	var ids = new Array();
        	nodes.forEach(function(node) {
        		ids.push(node.id);
        	});
        	
        	common.confirm("确认删除吗？", function() {
	        	$request.post(base_url + "/system/resource/batchDelete", {
	        		data: {
	        			ids: ids
	        		},
	        		confirm: common.confirm
	        	}, function() {
	        		treeObj.reAsyncChildNodes(null, "refresh", null);
	        	});
        	}, {title: "确认"});
        }
    </script>
	
	<th:block th:replace="headfoot/foot::list_ztree_foot">引用公共的homefoot</th:block>  
</html>			